/**
 * Lockbox
 * a Password and Secret Information Manager
 * View to display a table of LockboxItems
 *
 * Created by Nathan Black
 */

#import <CoreFoundation/CoreFoundation.h>
#import <Foundation/Foundation.h>
#import <GraphicsServices/GraphicsServices.h>
#import <UIKit/UIKit.h>
#import <UIKit/UIAlertSheet.h>
#import "LockboxPasswordView.h"


@interface LockboxPasswordView (Private)

-(void)pwViewFinished;

@end

@implementation LockboxPasswordView

- (void)dealloc
{
    [_background release];
    [_pwPrompt release];

    [super dealloc];
}

///////////// Constructor ////////////////
- (id)initWithFrame:(struct CGRect)frame app:(id)app state:(int)state
{
    self = [super initWithFrame:frame];
    if (self)
    {
        _storage = [LockboxStorage globalInstance];
        _delegate = app;
        _frame = frame;
        _state = state;
        
        CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
        
        // Allocate the background, but scoot it up a bit, so it alings with the transform that Springboard does.
        // Appears as if pwPrompt just pops up on the background as if it was Springboard's transition from the beginning. very sweet.
        UIImageView* background = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]] autorelease];
        CGPoint p;
        p.x = 0.0f; p.y = -20.0f; // TODO get exact size of status bar
        [background setOrigin:p];

        if (state == LBPP_AUTH) {
            _pwPrompt = [[LockboxPwPromptView alloc] initWithFrame:CGRectMake(LBPP_LEFT, LBPP_TOP_AUTH, LBPP_WIDTH, LBPP_HEIGHT_AUTH) promptState:state];
        }
        else {
            _pwPrompt = [[LockboxPwPromptView alloc] initWithFrame:CGRectMake(LBPP_LEFT, LBPP_TOP_NEW, LBPP_WIDTH, LBPP_HEIGHT_NEW) promptState:state];
        }
        [_pwPrompt setDelegate:self];
        [_pwPrompt setPwFieldFocus];

        struct CGSize kbSize = [UIKeyboard defaultSize];
        _keyboard = [[[UIKeyboard alloc] initWithFrame:CGRectMake(_frame.origin.x, _frame.size.height - kbSize.height, kbSize.width, kbSize.height)] autorelease];
        [[_keyboard defaultTextTraits] setAutoCapsType: 0];

        [self addSubview:background];
        [self addSubview:_keyboard];
        [self addSubview:_pwPrompt];

        CFRelease(colorSpace);
    }
    
    return self;
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button
{
    [sheet dismiss];
    [_pwPrompt clearFields];
}

- (void)passwordEntry:(NSString*)password
{
    NSLog(@"passwordEntry received.");
    @try {
        if (_state == LBPP_AUTH || _state == LBPP_NEW) {
            [_storage loadWithKey:password];
        }
        else {
            [_storage changeKey:password];
        }
        [_keyboard removeFromSuperview];
        [_delegate pwViewFinished];
        //[_pwPrompt clearFields];
    }
    @catch (NSException* ex) {
        NSLog(@"***Caught exception during loadWithKey/loadCSV: %@ reason: %@", [ex name], [ex reason]);
        UIAlertSheet *infoSheet = [[UIAlertSheet alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
        if ([ex name] == LockboxInvalidKeyException) {
            [infoSheet setBodyText:@"Invalid Password"];
        }
        else {
            NSString* msg = [NSString stringWithFormat:@"Error: %@", [ex reason]];
            [infoSheet setBodyText:msg];
        }
        [infoSheet addButtonWithTitle:@"OK"];
        [infoSheet setDelegate:self];
        [infoSheet setAlertSheetStyle:0];
        [infoSheet presentSheetFromAboveView:self];
    }

}

- (void)clearFields
{
    [_pwPrompt clearFields];
}

- (LockboxPwPromptState)state
{
    return _state;
}

/*- (NSMethodSignature*)methodSignatureForSelector:(SEL)selector {
     NSLog(@"LBPasswordView: Requested method for selector: %@", NSStringFromSelector(selector));
     return [super methodSignatureForSelector:selector];
}

- (BOOL)respondsToSelector:(SEL)aSelector {
    NSLog(@"LBPasswordView: Request for selector: %@", NSStringFromSelector(aSelector));
    return [super respondsToSelector:aSelector];
}

- (void)forwardInvocation:(NSInvocation *)anInvocation {
     NSLog(@"LBPasswordView: Called from: %@", NSStringFromSelector([anInvocation selector]));
    [super forwardInvocation:anInvocation];
}*/

@end
